package com.baiwei.vpnapi;
import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
import com.baiwei.vpnapi.utils.AppUtils;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.redis.core.StringRedisTemplate;
import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;


@SpringBootApplication
@MapperScan("com.baiwei.vpnapi")
public class VpnapiApplication {
    @Autowired
    StringRedisTemplate stringRedisTemplate;

    public static void initRules() {
        List<FlowRule> rules = new ArrayList<>();
        FlowRule rule = new FlowRule();
        rule.setResource("vpnapi");
        rule.setGrade(RuleConstant.FLOW_GRADE_QPS);
        // Set limit QPS to 20.
        rule.setCount(1000);
        rules.add(rule);
        FlowRuleManager.loadRules(rules);
    }

    @PostConstruct
    void started() {
        initRules();
        TimeZone.setDefault(TimeZone.getTimeZone("Asia/Shanghai"));
        System.out.println("---------------------------------");
        System.out.println(new Date());
        System.out.println("---------------------------------");
        stringRedisTemplate.opsForValue().set("adminToken", AppUtils.generateAuthCode());
    }
    public static void main(String[] args) {
        SpringApplication.run(VpnapiApplication.class, args);
    }
}
